/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.opgea.token.domain.entities.Company;
import com.opgea.token.domain.entities.Token;
import com.opgea.token.domain.entities.TokenDetails;
import com.opgea.token.domain.model.CompanyStatus;
import com.opgea.token.domain.model.TokenStatus;
import com.opgea.token.domain.qualifiers.TokenStatusQualifier;
import com.opgea.token.service.ApplicationService;
import com.opgea.token.util.HibernateUtil;

/**
 * 
 * @author Ramesh
 */
@Service
public class ApplicationServiceImpl implements ApplicationService {

    private Map<Long, CompanyStatus> companies = new HashMap<Long, CompanyStatus>();

    /**
     * Constructor
     */
    public ApplicationServiceImpl() {
	System.out.println("Application Service Constructor");
	Date date = Calendar.getInstance().getTime();
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	String dateString = dateFormat.format(date);
	Session session = HibernateUtil.getSessionFactory().openSession();
	@SuppressWarnings("unchecked")
	List<Company> companyList = session.createQuery("from Company").list();
	for (Company company : companyList) {
	    CompanyStatus companyStatus = new CompanyStatus(company.getId(),
		    company.getName());
	    companies.put(companyStatus.getCompanyId(), companyStatus);
	}
	@SuppressWarnings("unchecked")
	List<Token> tokenList = session.createQuery(
		"from Token t where t.tokenDetails.tokenStatus != '"
			+ TokenStatusQualifier.DONE.ordinal()
			+ "' and t.tokenDate = '" + dateString + "' ").list();
	for (Token token : tokenList) {
	    TokenStatus tokenStatus = new TokenStatus();
	    TokenDetails tokenDetails = token.getTokenDetails();
	    tokenStatus.setCompanyId(token.getCompany().getId());
	    tokenStatus.setTokenId(token.getId());
	    tokenStatus.setTokenNo(token.getTokenNo());
	    tokenStatus.setPersonName(tokenDetails.getName());
	    tokenStatus.setMobileNo(tokenDetails.getMobileNo().toString());
	    tokenStatus.setDestinationId(tokenDetails.getDestination().getId());
	    tokenStatus.setDestinationName(tokenDetails.getDestination()
		    .getName());
	    tokenStatus.setTokenStatus(token.getTokenDetails().getTokenStatus()
		    .ordinal());
	    tokenStatus.setAppointmentDate(token.getTokenDate());

	    if (companies.containsKey(tokenStatus.getCompanyId())) {
		CompanyStatus companyStatus = companies.get(tokenStatus
			.getCompanyId());
		companyStatus.getTokens().put(tokenStatus.getTokenId(),
			tokenStatus);
	    }
	}
    }

    /*
     * Company Authorization and Subscription of Product Status
     */
    @Override
    public CompanyStatus addCompanyStatus(CompanyStatus companyStatus) {
	if (companies.containsKey(companyStatus.getCompanyId())) {
	} else {
	    companies.put(companyStatus.getCompanyId(), companyStatus);
	}
	return companyStatus;
    }

    @Override
    public CompanyStatus removeCompanyStatus(Long companyId) {
	CompanyStatus companyStatus = null;
	if (!companies.isEmpty()) {
	    companyStatus = companies.get(companyId);
	    companies.remove(companyId);
	}
	return companyStatus;
    }

    @Override
    public CompanyStatus updateCompanyStatus(CompanyStatus companyStatus) {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Map<Long, CompanyStatus> findAllCompanies() {
	return this.companies;
    }

    @Override
    public CompanyStatus findCompany(Long id) {
	CompanyStatus companyStatus = companies.get(id);
	return companyStatus;
    }

    /*
     * Visitor Status
     */

    @Override
    public TokenStatus addTokenStatus(TokenDetails tokenDetails) {
	TokenStatus tokenStatus = new TokenStatus();
	Token token = tokenDetails.getToken();
	tokenStatus.setCompanyId(token.getCompany().getId());
	tokenStatus.setTokenId(token.getId());
	tokenStatus.setTokenNo(token.getTokenNo());
	tokenStatus.setPersonName(tokenDetails.getName());
	tokenStatus.setMobileNo(tokenDetails.getMobileNo().toString());
	tokenStatus.setDestinationId(tokenDetails.getDestination().getId());
	tokenStatus.setDestinationName(tokenDetails.getDestination().getName());
	tokenStatus.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
	tokenStatus.setAppointmentDate(token.getTokenDate());
	if (companies.containsKey(tokenStatus.getCompanyId())) {
	    CompanyStatus companyStatus = companies.get(tokenStatus
		    .getCompanyId());
	    if (companyStatus.getTokens().containsKey(tokenStatus.getTokenId())) {
	    } else {
		companyStatus.getTokens().put(tokenStatus.getTokenId(),
			tokenStatus);
		System.out
			.println("ApplicationServiceImpl >> addTokenStatus >> "
				+ tokenStatus.getTokenId() + " | "
				+ tokenStatus);
	    }
	}
	return tokenStatus;
    }

    @Override
    public TokenStatus updateTokenStatus(TokenDetails tokenDetails) {
	TokenStatus tokenStatus = new TokenStatus();
	Token token = tokenDetails.getToken();
	tokenStatus.setCompanyId(token.getCompany().getId());
	tokenStatus.setTokenId(token.getId());
	tokenStatus.setTokenNo(token.getTokenNo());
	tokenStatus.setPersonName(tokenDetails.getName());
	tokenStatus.setMobileNo(tokenDetails.getMobileNo().toString());
	tokenStatus.setDestinationId(tokenDetails.getDestination().getId());
	tokenStatus.setDestinationName(tokenDetails.getDestination().getName());
	tokenStatus.setAppointmentDate(token.getTokenDate());
	if (tokenDetails.getTokenStatus() != null) {
	    tokenStatus.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
	}
	if (companies.containsKey(tokenStatus.getCompanyId())) {
	    CompanyStatus companyStatus = companies.get(tokenStatus
		    .getCompanyId());
	    if (companyStatus.getTokens().containsKey(tokenStatus.getTokenId())) {
		companyStatus.getTokens().remove(tokenStatus.getTokenId());
		companyStatus.getTokens().put(tokenStatus.getTokenId(),
			tokenStatus);
	    } else {

	    }
	}
	return tokenStatus;
    }

    @Override
    public TokenStatus removeTokenStatus(TokenDetails tokenDetails) {
	TokenStatus tokenStatus = new TokenStatus();
	Token token = tokenDetails.getToken();
	tokenStatus.setCompanyId(token.getCompany().getId());
	tokenStatus.setTokenId(token.getId());
	tokenStatus.setTokenNo(token.getTokenNo());
	tokenStatus.setPersonName(tokenDetails.getName());
	tokenStatus.setMobileNo(tokenDetails.getMobileNo().toString());
	tokenStatus.setDestinationId(tokenDetails.getDestination().getId());
	tokenStatus.setDestinationName(tokenDetails.getDestination().getName());
	tokenStatus.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
	TokenStatus tokenSts = null;
	if (companies.containsKey(tokenStatus.getCompanyId())) {
	    CompanyStatus companyStatus = companies.get(tokenStatus
		    .getCompanyId());
	    if (companyStatus.getTokens().containsKey(tokenStatus.getTokenId())) {
		tokenSts = companyStatus.getTokens().get(
			tokenStatus.getTokenId());
		companyStatus.getTokens().remove(tokenStatus.getTokenId());
	    }
	}
	return tokenSts;
    }

    @Override
    public List<TokenStatus> findAllTokenStatus(Long companyId) {
	List<TokenStatus> tokens = new ArrayList<TokenStatus>();
	if (companies.containsKey(companyId)) {
	    List<TokenStatus> allTokens = null; // All tokens of a particular
						// company
	    CompanyStatus companyStatus = companies.get(companyId);
	    allTokens = new ArrayList<TokenStatus>(companyStatus.getTokens()
		    .values());
	    for (TokenStatus tokenStatus : allTokens) {

		Calendar todayCal = Calendar.getInstance();
		todayCal.setTime(new Date());
		todayCal.set(Calendar.HOUR, 0);
		todayCal.set(Calendar.HOUR_OF_DAY, 0);
		todayCal.set(Calendar.MINUTE, 0);
		todayCal.set(Calendar.SECOND, 0);
		todayCal.set(Calendar.MILLISECOND, 0);

		Calendar appointmentCal = Calendar.getInstance();
		appointmentCal.setTime(tokenStatus.getAppointmentDate());
		appointmentCal.set(Calendar.HOUR, 0);
		appointmentCal.set(Calendar.HOUR_OF_DAY, 0);
		appointmentCal.set(Calendar.MINUTE, 0);
		appointmentCal.set(Calendar.SECOND, 0);
		appointmentCal.set(Calendar.MILLISECOND, 0);

		/*
		 * System.out.println("Today: "+todayCal);
		 * System.out.println("ADate: "+appointmentCal);
		 * System.out.println("No. of visitor Status : "+tokens.size());
		 * System
		 * .out.println("Date Equals: "+todayCal.equals(appointmentCal
		 * ));
		 */
		System.out.println("Today Cal: " + todayCal);
		System.out.println("Appt Cal: " + appointmentCal);
		if (todayCal.compareTo(appointmentCal) == 0) {
		    System.out.println("TokenStatus : " + tokenStatus);
		    tokens.add(tokenStatus);
		}
	    }

	}
	return tokens;
    }

    @Override
    public List<TokenStatus> findAllTokenStatusByDestination(Long companyId,
	    Long destinationId) {
	List<TokenStatus> tokens = new ArrayList<TokenStatus>();
	if (companies.containsKey(companyId)) {
	    List<TokenStatus> allTokens = null; // All tokens of a particular
						// company
	    CompanyStatus companyStatus = companies.get(companyId);
	    allTokens = new ArrayList<TokenStatus>(companyStatus.getTokens()
		    .values());
	    for (TokenStatus tokenStatus : allTokens) {

		Calendar todayCal = Calendar.getInstance();
		todayCal.setTime(new Date());
		todayCal.set(Calendar.HOUR, 0);
		todayCal.set(Calendar.HOUR_OF_DAY, 0);
		todayCal.set(Calendar.MINUTE, 0);
		todayCal.set(Calendar.SECOND, 0);
		todayCal.set(Calendar.MILLISECOND, 0);

		Calendar appointmentCal = Calendar.getInstance();
		appointmentCal.setTime(tokenStatus.getAppointmentDate());
		appointmentCal.set(Calendar.HOUR, 0);
		appointmentCal.set(Calendar.HOUR_OF_DAY, 0);
		appointmentCal.set(Calendar.MINUTE, 0);
		appointmentCal.set(Calendar.SECOND, 0);
		appointmentCal.set(Calendar.MILLISECOND, 0);

		/*
		 * System.out.println("Today: "+todayCal);
		 * System.out.println("ADate: "+appointmentCal);
		 * System.out.println("No. of visitor Status : "+tokens.size());
		 * System
		 * .out.println("Date Equals: "+todayCal.equals(appointmentCal
		 * ));
		 */
		System.out.println("Today Cal: " + todayCal);
		System.out.println("Appt Cal: " + appointmentCal);
		if (tokenStatus.getDestinationId().equals(destinationId)
			&& todayCal.compareTo(appointmentCal) == 0) {
		    System.out.println("TokenStatus : " + tokenStatus);
		    tokens.add(tokenStatus);
		}
	    }

	}
	return tokens;
    }

}
